package com.monster.servlet.home;

import com.monster.dao.UserLikeDao;
import com.monster.dao.UserLikeDaoImpl;
import com.monster.service.UserLikeService;
import com.monster.service.UserLikeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/UserLikeServlet")
public class UserLikeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String no1 = request.getParameter("likeId");
        Long likeId = Long.parseLong(no1);
        String no2 = request.getParameter("userId");
        Long userId = Long.parseLong(no2);
        String no3 = request.getParameter("likeType");
        int likeType = Integer.parseInt(no3);
        String result = null;;
        switch (likeType){
            //歌曲类型收藏
            case 1:
                result = combine(userId,likeType,likeId);
                break;
            //专辑类型收藏
            case 2:
                result = combine(userId,likeType,likeId);
                break;
            //歌单类型收藏
            case 3:
                result = combine(userId,likeType,likeId);
                break;
        }
        out.print(result);
        out.flush();
        out.close();
    }

    //查询是否存在，存在调用删除，不存在调用添加
    public String combine (Long userId, int likeType, Long likeId){
        String result = null;
        if(exist(userId,likeType,likeId)==true){
            if(delete(userId,likeType,likeId)==true){
                result = "{\"data\":\"NO\"}";
            }else {
                result = "{\"data\":\"ERROR\"}";
            }
        }else{
            if(add(userId,likeType,likeId)==true) {
                result = "{\"data\":\"YES\"}";
            }else {
                result = "{\"data\":\"ERROR\"}";
            }
        }
        return  result;
    }

    //查看该收藏是否存在
    public boolean exist (Long userId, int likeType, Long likeId){
        UserLikeService userLikeService = new UserLikeServiceImpl();
        boolean flag = false;
        int result = 0 ;
        result = userLikeService.getLikeCountByID(userId,likeType,likeId);
        if(result==1){
            flag=true;
            return flag;
        }
        else{
            return flag;
        }
    }

    //取消收藏
    public boolean delete (Long userId, int likeType, Long likeId){
        UserLikeService userLikeService = new UserLikeServiceImpl();
        boolean flag = false;
        int result = 0 ;
        result = userLikeService.deleteLikeById(userId,likeType,likeId);
        if(result==1){
            flag=true;
            return flag;
        }
        else{
            return flag;
        }
    }

    //添加收藏
    public boolean add (Long userId, int likeType, Long likeId){
        UserLikeService userLikeService = new UserLikeServiceImpl();
        boolean flag = false;
        int result = 0 ;
        result = userLikeService.addLikeByID(userId,likeType,likeId);
        if(result==1){
            flag=true;
            return flag;
        }
        else{
            return flag;
        }
    }

}

